package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"hema-fresh/hemarpc/models/mysql"

	"hema-fresh/hemarpc/hemarpc"
	"hema-fresh/hemarpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductUpdateLogic {
	return &ProductUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *ProductUpdateLogic) ProductUpdate(in *hemarpc.ProductUpdateRequest) (*hemarpc.ProductUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var hema mysql.HemaProduct
	err := hema.ProductDetail(mysql.DB, in.HemaProductId)
	if err != nil {
		return nil, errors.New("不存在该数据,修改失败")
	}
	hema = mysql.HemaProduct{
		Model:        gorm.Model{ID: uint(in.HemaProductId)},
		ProductName:  in.ProductName,
		ProductPrice: float64(in.ProductPrice),
		ProductStock: int(in.ProductStock),
		HemaTypeId:   int(in.HemaTypeId),
	}
	err = hema.ProductUpdate(mysql.DB)
	if err != nil {
		return nil, err
	}

	return &hemarpc.ProductUpdateResponse{
		HemaProductId: int64(hema.ID),
	}, nil
}
